package com.link.base.base.apilog.service;

import com.link.base.base.apilog.dao.mybatis.mapper.ApiLogLineMapper;
import com.link.base.base.apilog.model.ApiLogLine;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * api接口调用日志记录行service
 * @author 许博文
 * @date 2019/12/25
 */
@Service
public class ApiLogLineServiceImpl extends BasicServiceImpl<ApiLogLine> implements ApiLogLineService {

    @Resource
    private ApiLogLineMapper apiLogLineMapper;

    @Override
    public BasicMapper<ApiLogLine> getBasicMapper() {
        return apiLogLineMapper;
    }

    @Override
    public List<ApiLogLine> queryNumTopN(ApiLogLine entity) throws Exception {
        return apiLogLineMapper.queryNumTopN(entity);
    }

    @Override
    public List<ApiLogLine> queryTotalNum(ApiLogLine entity) throws Exception {
        return apiLogLineMapper.queryTotalNum(entity);
    }

    @Override
    public List<ApiLogLine> queryFailureTopN(ApiLogLine entity) throws Exception {
        return apiLogLineMapper.queryFailureTopN(entity);
    }

    @Override
    public List<ApiLogLine> queryFailureNum(ApiLogLine entity) throws Exception {
        return apiLogLineMapper.queryFailureNum(entity);
    }

    @Override
    public ApiLogLine queryRatio(ApiLogLine entity) throws Exception {
        return apiLogLineMapper.queryRatio(entity);
    }

    @Override
    public ApiLogLine queryTotalHeadNum(ApiLogLine entity) throws Exception {
        return apiLogLineMapper.queryTotalHeadNum(entity);
    }

    @Override
    public Map<String, Long> queryTotalAndSuccess(ApiLogLine entity) throws Exception {

        String startTime = entity.getStartTime();
        String endTime = entity.getEndTime();

        Date startDate = DateUtil.stringToDate(startTime, "yyyy-MM-dd");
        Date endDate = DateUtil.stringToDate(endTime, "yyyy-MM-dd");

        String endStr = DateUtil.dateToStr(endDate, "yyyy-MM-dd HH:mm:ss");

        long milisDiff = endDate.getTime() - startDate.getTime();
        long minDiff = milisDiff / 1000 / 60 / 50;
        System.out.println(minDiff);
        //ceil 时间间隔分钟数
        int ceil = (int) (Math.ceil(minDiff));
        Map result = new LinkedHashMap<>();
        entity.setTimeBlockNum(ceil);
        List<ApiLogLine> apiLogLines = apiLogLineMapper.queryTotalAndSuccess(entity);

        //获取指定时间段内，划分为50段的时间点，并与数据库返回的分组查询结果做比较。一样则取值
        Calendar nowTime = Calendar.getInstance();
        for (int i = 0; i <= 52; i++) {
            Date tmpDate = new Date(startDate.getTime());
            nowTime.setTime(tmpDate);
            nowTime.add(Calendar.MINUTE, i * ceil);
            tmpDate.setTime(nowTime.getTimeInMillis());
            String tmpStr = DateUtil.dateToStr(tmpDate, "yyyy-MM-dd HH:mm");
            Long num = 0L;
            for (ApiLogLine item : apiLogLines) {
                if (item.getTime().equals(tmpStr)) {
                    num = item.getTotalNum();
                }
            }
            if (tmpDate.after(endDate) || tmpStr.equals(endStr)) {
                result.put(endStr, 0);
                break;
            }
            System.out.println(tmpDate.getTime());
            result.put(tmpStr, num);
        }
        return result;
    }

    /**
     * @param
     * @return
     * @throws Exception 报错
     * @auther HuangLJ
     * @date 2018/10/15 3:52 PM
     * @comments 查询指定时间段内，接口报错最多的10个clientId
     */
    @Override
    public List<ApiLogLine> queryClientFailureTopN(ApiLogLine entity) throws Exception {
        List<ApiLogLine> retApiLineList = apiLogLineMapper.queryClientFailureTopN(entity);
        if (retApiLineList != null) {
            //正序排列
            retApiLineList.sort(Comparator.comparing(ApiLogLine::getTotalNum));
        }
        return retApiLineList;
    }

    @Override
    public void insertApiLogLine(ApiLogLine apiLogLine) throws Exception {
        apiLogLineMapper.insert(apiLogLine);
    }

    /**
     * 删除历史日志
     *
     * @author huanglongjian
     * @date 2019-09-28 23:17
     */
    @Override
    public void deleteHistory() throws Exception {
        apiLogLineMapper.deleteHistory();
    }
}
